package com.limingru.newbee.service.impl;

import com.limingru.newbee.api.vo.IndexConfigGoodsVO;
import com.limingru.newbee.common.IndexConfigEnum;
import com.limingru.newbee.dao.GoodsMapper;
import com.limingru.newbee.dao.IndexConfigMapper;
import com.limingru.newbee.entity.Goods;
import com.limingru.newbee.entity.IndexConfig;
import com.limingru.newbee.service.IndexConfigService;
import com.limingru.newbee.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndexConfigServiceImpl implements IndexConfigService {
    @Autowired
    IndexConfigMapper indexConfigMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<IndexConfigGoodsVO> getConfigGoods(int configType) {
        List<IndexConfigGoodsVO> indexConfigGoodsVOS = new ArrayList<>();
        List<IndexConfig> indexConfigs = indexConfigMapper.selectIndexConfigByConfigType(configType);
        if (!CollectionUtils.isEmpty(indexConfigs)) {
            List<Long> goodsIds = indexConfigs.stream().map(IndexConfig::getGoodsId).collect(Collectors.toList());
            List<Goods> goods = goodsMapper.selectGoodsByGoodsIds(goodsIds);
            return BeanUtil.copyList(goods, IndexConfigGoodsVO.class);
        }
        return indexConfigGoodsVOS;
    }
}
